package com.wkbb.sports.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wkbb.common.base.BaseServiceImpl;
import com.wkbb.common.constants.Constants;
import com.wkbb.common.dto.wx.AccountDto;
import com.wkbb.common.dto.wx.AccountInfoDto;
import com.wkbb.sports.mapper.AccountMapper;
import com.wkbb.sports.model.Account;
import com.wkbb.sports.service.AccountService;

import lombok.extern.slf4j.Slf4j;

/**
 * 管理服务接口
 *
 * @author 
 * 创建时间：2019年6月27日
 */
@Service
@Slf4j
public class AccountServiceImpl extends BaseServiceImpl<Account> implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private void setMapper(AccountMapper accountMapper) {
        super.setMapper(accountMapper);
    }

    @Override
    public PageInfo<AccountInfoDto> getAccountDtoPageList(Map<String, Object> params, Integer pageNo, Integer pageSize) {
        if (pageNo == null || pageSize == null) {
            return null;
        }
        Page<AccountInfoDto> page = PageHelper.startPage(pageNo, pageSize, true);
        PageInfo<AccountInfoDto> pageInfo = new PageInfo<>(accountMapper.getAccountDtoPageList(params));
        pageInfo.setTotal(page.getTotal());
        return pageInfo;
    }

    @Override
    public void addAccount(AccountDto accountDto) throws Exception {
        Account account = new Account();
        BeanUtils.copyProperties(accountDto, account);
        insert(account);
    }

    @Override
    public void updateAccount(AccountDto accountDto) throws Exception {
        Account account = new Account();
        BeanUtils.copyProperties(accountDto, account);
        update(account);
    }

    @Override
    public List<Account> getAccountList(Map<String, Object> params)
    {
        return accountMapper.getAccountList(params);
    }

    @Override
    public List<AccountDto> getAccountDtoList(Map<String, Object> params) throws Exception{
        return accountMapper.getAccountDtoList(params);
    }

    @Override
    @Transactional
    public void deleteWxAccount(Long orgId) {
        //先查询出来包含此orgId的Account账户,判断jsonArray长度,大于1,update,=1直接delete
        Map<String,Object> paramMap = new HashMap();
        paramMap.put("orgId",orgId);
        Account account = accountMapper.getByMapParams(paramMap);
        if(null != account){
            JSONArray orgJsonArr = JSONObject.parseArray( JSONObject.toJSONString(account.getOrgInfo()));
            if(!orgJsonArr.isEmpty()){
                if(orgJsonArr.size()==1){
                    JSONObject json = (JSONObject)orgJsonArr.get(0);
                    accountMapper.deleteById(account.getId());
                }else if(orgJsonArr.size()>1){
                    //循环jsonArray
                    for(int i=0;i<orgJsonArr.size();i++) {
                        JSONObject job = orgJsonArr.getJSONObject(i);
                        if(new Long(job.get("orgId").toString()).longValue() == orgId.longValue()){
                            orgJsonArr.remove(job);
                        }
                    }
                    account.setOrgInfo(orgJsonArr);
                    accountMapper.update(account);
                }
            }
        }
    }

    @Override
    public List<AccountInfoDto> getAccountInfoList(String orgIds)
    {
        List<Long> idList = new ArrayList();

        String[] orgArr = orgIds.split(Constants.SEPARATOR_MARK);
        for (int i = 0; i < orgArr.length; i++) {
            idList.add(Long.valueOf(orgArr[i]));
        }

        if(!idList.isEmpty())
        {
            return accountMapper.getAccountInfoList(idList);
        }

        return null;
    }

    @Override
    public AccountInfoDto getAccountInfo(Long accountId)
    {
        return accountMapper.getAccountInfo(accountId);
    }

}
